<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UserRequest;
use App\Http\Requests\Admin\UserStoreRequest;
use App\Http\Services\Admin\UserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(UserRequest $request, UserService $userService): JsonResponse
    {
        $params = $request->validated();
        return success(data: $userService->table($params['page'] ?? 1, $params['pageSize'] ?? 10, $params));
    }

    /**
     * 创建
     * @throws \Throwable
     */
    public function store(UserStoreRequest $request, UserService $userService): JsonResponse
    {
        $params = $request->validated();
        $userService->insertOrUpdate(0, $params);
        return success();
    }

    /**
     * 查看详情
     */
    public function show(int $id, UserService $userService): JsonResponse
    {
        return success();
    }

    /**
     * 更新
     * @throws \Throwable
     */
    public function update(int $id, UserStoreRequest $request, UserService $userService): JsonResponse
    {
        $params = $request->validated();
        $userService->insertOrUpdate($id, $params);
        return success();
    }

    public function resetPassword(int $id, UserService $userService): JsonResponse
    {
        $userService->resetPassword($id);
        return success();
    }

    /**
     * 删除
     * @throws \Throwable
     */
    public function destroy(Request $request, UserService $userService): JsonResponse
    {
        $params = $this->validate($request, [
            'ids' => 'required|array',
            'ids.*' => 'required|integer'
        ]);
        $userService->delete($params['ids']);
        return success();
    }
}
